<?php 
	class Login extends CI_Controller {
		public function __construct() {
			parent:: __construct();
			$this->load->helper("url");
			$this->load->library("my_layout"); // Sử dụng thư viện layout
			$this->my_layout->setLayout("layout/frontend"); // load file layout chính (view/layout/frontend.php)
			$this->load->library('session');
			$this->load->helper(array('form', 'url'));
	        $this->load->library('form_validation');
	        $this->load->model('Musers');
		}

		public function index($msg = NULL){
			$this->form_validation->set_rules('username', 'Username', 'required');
	        $this->form_validation->set_rules('password', 'Password', 'required');
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
 			$data['title'] = 'Đăng Nhập';
 			$data['description']= "";
	        $data['msg'] = $msg;
			if ($this->form_validation->run() == FALSE)
	        {
	           $this->my_layout->view("frontend/login",$data);
	        } else {
		        $result = $this->Musers->validate();
		        if(! $result){
		            $msg = "<div class='error'>Username hay Password không đúng</div>";
		            $data['msg'] = $msg;
		            $this->my_layout->view("frontend/login",$data);
		        } else {
		            redirect('home');
		        }        
	        }
	    }
	    public function do_logout(){
	        $this->session->sess_destroy();
	        redirect('login');
	    }

	    public function getIp(){

        $ip = $_SERVER['REMOTE_ADDR'];     
        if($ip){
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            }
            return $ip;
        }
        // There might not be any data
        return false;
    }
	}
?>
